package www.wxwnb.clerk.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import www.wxwnb.clerk.application.MyApplication;

/**
 *
 */

public class Customer {

    public static final String CUSTONER_INFO = "customer_info";
    private final SharedPreferences mSharedPreferences;
    private final String LOGIN_NAME = "loginName";
    private final String PASSWORD = "password";
    private final String USER_ID = "userId";
    private final String TYPE = "type";
    private final String USER_NAME = "userName";
    private final String MONEY = "money";
    private final String JOB_NUMBER = "jobNumber";


    /**
     * 登陆姓名
     */
    private String mLoginName;
    private String mPassword;
    private String mUserId;
    private String mType;
    private String mUserName;
    private String mMoney;
    private String mJobNumber;


    private Customer() {
        mSharedPreferences = MyApplication.getContext().getSharedPreferences(CUSTONER_INFO, Context.MODE_PRIVATE);
    }

    private static Customer customer = null;

    public static Customer getInstance() {
        if (customer == null) {
            customer = new Customer();
        }
        return customer;
    }

    public String getLoginName() {
        //如果为null或者""的话，取出默认值
        if (TextUtils.isEmpty(mLoginName)) {
            mLoginName = mSharedPreferences.getString(LOGIN_NAME, "");
        }
        return mLoginName;
    }

    public void setLoginName(String loginName) {
        //保存到本地
        if (TextUtils.isEmpty(mLoginName) || !mLoginName.equals(loginName)) {
            SharedPreferences.Editor edit = mSharedPreferences.edit();
            edit.putString(LOGIN_NAME, loginName);
            edit.commit();
        }
        //保存到成员变量
        mLoginName = loginName;
    }

    public String getPassword() {
        if (TextUtils.isEmpty(mPassword)) {
            mPassword = mSharedPreferences.getString(PASSWORD, "");
        }
        return mPassword;
    }

    public void setPassword(String password) {
        //保存到本地
        if (TextUtils.isEmpty(mPassword) || !mPassword.equals(password)) {
            SharedPreferences.Editor edit = mSharedPreferences.edit();
            edit.putString(PASSWORD, password);
            edit.commit();
        }
        //保存到成员变量
        mPassword = password;
    }

    public String getUserId() {
        if (TextUtils.isEmpty(mUserId)) {
            mUserId = mSharedPreferences.getString(USER_ID, "");
        }
        return mUserId;
    }

    public void setUserId(String userId) {
        //保存到本地
        if (TextUtils.isEmpty(mUserId) || !mUserId.equals(userId)) {
            SharedPreferences.Editor edit = mSharedPreferences.edit();
            edit.putString(USER_ID, userId);
            edit.commit();
        }
        mUserId = userId;
    }

    public String getType() {
        if (TextUtils.isEmpty(mType)) {
            mType = mSharedPreferences.getString(TYPE, "");
        }
        return mType;
    }

    public void setType(String type) {
        if (TextUtils.isEmpty(mType) || !mType.equals(type)) {
            SharedPreferences.Editor edit = mSharedPreferences.edit();
            edit.putString(TYPE, type);
            edit.commit();
        }
        mType = type;
    }

    public String getUserName() {
        if (TextUtils.isEmpty(mUserName)) {
            mUserName = mSharedPreferences.getString(USER_NAME, "");
        }
        return mUserName;
    }

    public void setUserName(String userName) {
        if (TextUtils.isEmpty(mUserName) || !mUserName.equals(userName)) {
            SharedPreferences.Editor edit = mSharedPreferences.edit();
            edit.putString(USER_NAME, userName);
            edit.commit();
        }
        mUserName = userName;
    }

    public String getMoney() {
        if (TextUtils.isEmpty(mMoney)) {
            mMoney = mSharedPreferences.getString(MONEY, "");
        }
        return mMoney;
    }

    public void setMoney(String money) {
        if (TextUtils.isEmpty(mMoney) || !mMoney.equals(money)) {
            SharedPreferences.Editor edit = mSharedPreferences.edit();
            edit.putString(MONEY, money);
            edit.commit();
        }
        mMoney = money;
    }

    //取出默认值
    public String getJobNumber() {
        if (TextUtils.isEmpty(mJobNumber)) {
            mJobNumber = mSharedPreferences.getString(JOB_NUMBER, "");
        }
        return mJobNumber;
    }

    public void setjobNumber(String jobNumber) {
        if (TextUtils.isEmpty(mJobNumber) || !mJobNumber.equals(jobNumber)) {
            SharedPreferences.Editor edit = mSharedPreferences.edit();
            edit.putString(JOB_NUMBER, jobNumber);
            edit.commit();
        }
        mJobNumber = jobNumber;
    }

    public void clear() {
        mLoginName = null;
        mPassword = null;
        mUserId = null;
        mType = null;
        mUserName = null;
        mJobNumber=null;
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.clear();
        editor.commit();
    }
}
